package guoanmaker.operator.business.controller;

import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.OperatorNewsList;
import guoanmaker.operator.business.service.OperatorNewsListServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

/**
 * 新闻表
* @ClassName: OperatorNewsListController
* @Description:TODO (这里用一句话描述这个类的作用) 
* @author lihongyu
* @date 2017年8月23日 下午3:31:15 
*  
 */
@RestController
@RequestMapping(ApiController.OPERATOR_NEWS_LIST_URL)
public class OperatorNewsListController {
    private final OperatorNewsListServiceInterface operatorNewsListServiceInterface;

    public OperatorNewsListController(OperatorNewsListServiceInterface operatorNewsListServiceInterface) {
        this.operatorNewsListServiceInterface = operatorNewsListServiceInterface;
    }

    /** 
    * @Title: saveNewsList
    * @Description: 新增新闻 
    * @param: OperatorNewsList   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存新闻", notes = "新增新闻")
    @PostMapping(value = "saveNewsList")
    public AjaxJson saveNewsList(@RequestBody OperatorNewsList operatorNewsList) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try {
    		int a=operatorNewsListServiceInterface.saveNewsList(operatorNewsList);
    		if(a==1){
    			ajaxJson.setSuccess(true).setStatus(200).setData("保存成功");
    		}
    		else if(a==-1){
    			ajaxJson.setSuccess(true).setStatus(250).setData("新闻标题已存在");
    		}
    		else if(a==-2){
    			ajaxJson.setSuccess(true).setStatus(250).setData("改原因已经改变或删除请刷新页面重试");
    		}
    		else{
    			ajaxJson.setSuccess(true).setStatus(250).setData("保存失败");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("保存失败");
		}
        return ajaxJson;
    }
    /** 
     * @Title: saveNewsList
     * @Description: 修改新闻 
     * @param: OperatorNewsList   
     * @return AjaxJson    
     * @throws 
     */
    @ApiOperation(value = "修改新闻状态", notes = "修改新闻状态")
    @PostMapping(value = "updateStautsNewsList")
    public AjaxJson updateStautsNewsList(String id,Integer stauts) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorNewsListServiceInterface.updateStautsNewsList(id, stauts));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("操作失敗");
		}
    	return ajaxJson;
    }

    /** 
     * @Title: deleteNewsListById
     * @Description: 根据Id删除新闻
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除新闻", notes = "根据Id删除新闻")
    @PostMapping(value = "deleteNews")
   public AjaxJson deleteNews(String id) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try {
    		operatorNewsListServiceInterface.deleteNewsListById(id);
    		ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("数据删除失败!");
		}
        
        return ajaxJson;
    }

    /**
     * @return OperatorNewsList    
     * @Title: getNewsListById 
     * @Description: 根据Id查询新闻
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询新闻", notes = "根据Id查询新闻")
    @PostMapping(value = "getNewsListById")
    public AjaxJson getNewsListById(String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorNewsListServiceInterface.getNewsListById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllNewsList
     * @Description: 查找所有新闻 
     * @throws 
     */
    @ApiOperation(value = "查找所有新闻", notes = "查找所有新闻")
    @PostMapping(value = "getAllNewsList")
  
    public AjaxJson getAllNewsList() {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorNewsListServiceInterface.getAllNewsList());
    }

    /**
     * @return AjaxJson   
     * @Title: getAllNewsListByPage                       //此接口没有用到
     * @Description: 查询所有新闻并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询新闻", notes = "分页查询新闻")
    
    @PostMapping(value = "getAllNewsListByPage")
    public AjaxJson getAllNewsListByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorNewsListServiceInterface.getAllNewsListByPage(page, size));
    }
    
    /**
     * @return AjaxJson   
     * @Title: getAllNewsListByPage 
     * @Description: 根据状态分页查询新闻
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "根据状态分页查询新闻", notes = "根据状态分页查询新闻")
    
    @PostMapping(value = "getAllNewsListByStautsPage")
    public AjaxJson getAllNewsListByStautsPage(Integer page,Integer size,Integer status,String newsTitle,String newsCreater,String StrartCreateDate,
    		String endCreateDate,String StrartNewsDate,String endNewsDate) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try {
    		Map<String,Object> map= operatorNewsListServiceInterface.getAllNewsListByStautsPage( page, size, status, newsTitle, newsCreater, StrartCreateDate,
           		 endCreateDate, StrartNewsDate, endNewsDate);
   		    int a=(int) map.get("count");
	   		if(a>0){
	   			ajaxJson.setSuccess(true).setStatus(200).setData(map);
	   	    }else{
	   	        ajaxJson.setSuccess(false).setStatus(250).setData("没有查询出数据");
	   	    }
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(250).setData("查詢失敗");
		}
		return  ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: getTitleAndUrl 
     * @Description: 获取新闻标题与URL
     */
    @ApiOperation(value = "获取新闻列表app", notes = "获取新闻列表app")
    @GetMapping(value = "getTitleAndUrl")
    public AjaxJson getTitleAndUrl(@RequestParam(value = "page", defaultValue = "0") Integer page,@RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	Page<OperatorNewsList> pnews=operatorNewsListServiceInterface.getTieleAndUrl(page,size);
    	ajaxJson.setSuccess(true).setStatus(200).setData(pnews);
    		
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据").setData(null);
    	}
    	
    	return ajaxJson;
    }
    
    
    /**
     * @return AjaxJson   
     * @Title: getTitleAndUrl 
     * @Description: 获取新闻标题与URL
     */
    @ApiOperation(value = "获取新闻列表pc", notes = "获取新闻列表pc")
    @GetMapping(value = "getpcNewList")
    public AjaxJson getpcNewList(@RequestParam(value = "page", defaultValue = "0") Integer page,@RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	Map remap=operatorNewsListServiceInterface.getpcNewList(page,size);
    	ajaxJson.setSuccess(true).setStatus(200).setData(remap);
    		
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据").setData(null);
    	}
    	
    	return ajaxJson;
    }
    
    /** 
     * @Title: updateNewList
     * @Description: 编辑新闻 
     * @param: OperatorNewsList   
     * @return AjaxJson    
     * @throws 
     */
     @ApiOperation(value = "编辑新闻", notes = "编辑新闻")
     @PostMapping(value = "updateNewList")
     public AjaxJson updateNewList(@RequestBody OperatorNewsList operatorNewsList) {
    	 AjaxJson ajaxJson=new AjaxJson();
    	 try {
     		int a=operatorNewsListServiceInterface.updateNewList(operatorNewsList);
     		if(a==1){
     			ajaxJson.setSuccess(true).setStatus(200).setData("修改成功");
     		}
     		else if(a==0){
     			ajaxJson.setSuccess(true).setStatus(250).setData("修改失败");
     		}
     		else if(a==-1){
     			ajaxJson.setSuccess(true).setStatus(250).setData("新闻标题已存在");
     		}
     		else{
     			ajaxJson.setSuccess(true).setStatus(250).setData("该新闻种类已修改或删除请重新访问页面");
     		}
 		} catch (Exception e) {
 			e.printStackTrace();
 			ajaxJson.setSuccess(true).setStatus(250).setData("修改失败");
 		}
         return ajaxJson;
     }

}
